package com.mc.moring.widge;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.mc.moring.R;
import com.mc.moring.app.LogUtils;
import com.mc.moring.base.HttpCallback;
import com.mc.moring.bean.AppConfigBean;
import com.mc.moring.bean.HomePageCreditInfo;
import com.mc.moring.main.modle.MianModle;
import com.mc.moring.util.GsonUtils;
import com.mc.moring.util.StringUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: ZhiLiProject
 * @Package: com.xx.text.widget
 * @ClassName: DialogView
 * @Author: ANWEN
 * @CreateDate: 12/12/23 7:52 PM
 * @UpdateUser:
 * @UpdateDate: 12/12/23 7:52 PM
 * @UpdateRemark:
 * @Version:
 */
public class CommonRetentionDialog extends Dialog {

    DialogViewOnClickListener dialogViewOnClickListener;

    private Context mContext;
    private TextView tv_abandonar;
    private TextView tv_confirmar;

    public CommonRetentionDialog(@NonNull Context context) {
        super(context, R.style.JDialog_Them);
        mContext = context;
    }

    public CommonRetentionDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected CommonRetentionDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public interface DialogViewOnClickListener{
        void onClickConfirm();
        void onClickCancel();
    }

    public void setDialogViewOnClickListener(DialogViewOnClickListener dialogViewOnClickListener){
        this.dialogViewOnClickListener = dialogViewOnClickListener;
    }

    public void setMagin(Activity activity, View layoutParent, int number){
        if (layoutParent.getLayoutParams() instanceof LinearLayout.LayoutParams) {
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) layoutParent.getLayoutParams();
            lp.width = getScreenWidth(activity) - number;
            layoutParent.setLayoutParams(lp);
        }else if(layoutParent.getLayoutParams() instanceof FrameLayout.LayoutParams){
            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) layoutParent.getLayoutParams();
            lp.width = getScreenWidth(activity) - number;
            layoutParent.setLayoutParams(lp);
        }
    }

    public static int getScreenWidth(Activity context){
        Display display = context.getWindowManager().getDefaultDisplay();	//获得默认（本地）的显示设备
        int screen_width = display.getWidth();
        return screen_width;
    }

    private boolean cancel;

    private HomePageCreditInfo homePageCreditInfo;
    @Override
    public void setCanceledOnTouchOutside(boolean cancel) {
        this.cancel = cancel;
        super.setCanceledOnTouchOutside(cancel);
    }

    @Override
    public void onBackPressed() {
        if (!cancel){
            return;
        }
        super.onBackPressed();
    }

    public void showToRetentionDialog(Context context) {
        View view = View.inflate(context,R.layout.dialog_common_retention, null);
        Window window = this.getWindow();

        window.setBackgroundDrawableResource(R.color.transparent);
        TextView textview_text = view.findViewById(R.id.textview_text);
        TextView tv_home_amount = view.findViewById(R.id.tv_home_amount);
        securitygetApplicationConfig(tv_home_amount);
        tv_abandonar = view.findViewById(R.id.tv_abandonar);
        tv_confirmar = view.findViewById(R.id.tv_confirmar);
        setStyle();

        CommonRetentionDialog myDialog = new CommonRetentionDialog(context);
        myDialog.setContentView(view);
        myDialog.show();

        tv_confirmar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(dialogViewOnClickListener!=null){
                    dialogViewOnClickListener.onClickConfirm();
                }
                myDialog.dismiss();
            }
        });
        tv_abandonar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(dialogViewOnClickListener!=null){
                    dialogViewOnClickListener.onClickCancel();
                }
                myDialog.dismiss();
            }
        });
    }

    private void setStyle(){
        Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "Poppins-Medium.ttf");
        tv_abandonar.setTypeface(typeface);
        tv_confirmar.setTypeface(typeface);
    }


    private void securitygetApplicationConfig(TextView tv_home_amount) {
        Map<String, String> params = new HashMap<>();
        MianModle.Companion.getInstant().securitygetApplicationConfig(params, new HttpCallback() {
            @Override
            public void onFaill(@NonNull String e) {

            }
            @Override
            public void onSuccess(@NonNull String data) {
                LogUtils.logE("securitygetApplicationConfig data:"+data);
                try {
                    JSONObject jsonObject = new JSONObject(data);
                    if (MianModle.Companion.getInstant().isSuccess(jsonObject)) {
                        AppConfigBean appConfigBean = GsonUtils.INSTANCE.getGson().fromJson(data, AppConfigBean.class);
                        String homeAmount = appConfigBean.getData().getUserInfoHomeAmount();
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                if (!TextUtils.isEmpty(homeAmount)) {
                                    if (tv_home_amount != null) {
                                        String showText = "Alcanza hasta $"+ StringUtils.formatNumberWithCommas(homeAmount);
                                        tv_home_amount.setText(showText);
                                    }

                                }
                            }
                        });
                    }

                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }



//    public static String formatNumberWithCommas(String input) {
//        try {
//            double number = Double.parseDouble(input);
//            NumberFormat numFormat = NumberFormat.getNumberInstance();
//            numFormat.setGroupingUsed(true);
//            numFormat.format(number);
//            return numFormat.format(number);
//        } catch (NumberFormatException e) {
//            e.printStackTrace();
//            return input;
//        }
//    }

}
